package com.tianzj.mvc.system.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.tianzj.mvc.system.dao.SysRoleMapper;
import com.tianzj.mvc.system.pojo.SysRole;
import com.tianzj.mvc.system.pojo.SysRoleExample;
import com.tianzj.mvc.system.service.SysRoleService;
import com.tianzj.util.Page;
import com.tianzj.util.SnowflakeIdWorker;

@Service
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	public SysRoleExample getExample(SysRole sysRole){
		SysRoleExample example=new SysRoleExample();
		SysRoleExample.Criteria criteria = example.createCriteria();
		if(sysRole.getId()!=null){
			criteria.andIdEqualTo(sysRole.getId());
		}
		if(StringUtils.isNotEmpty(sysRole.getRoleName())){
			criteria.andRoleNameEqualTo(sysRole.getRoleName());
		}
		if(sysRole.getStatus()!=null){
			criteria.andStatusEqualTo(sysRole.getStatus());
		}
		return example;
	}
	
	@Override
	public List<SysRole> getSysRoleList(Page page, SysRole sysRole) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		return sysRoleMapper.selectByExample(getExample(sysRole));
	}

	@Override
	public void addRole(SysRole sysRole) {
		sysRole.setId(new SnowflakeIdWorker(0, 0).nextId());
		sysRole.setCreateDate(System.currentTimeMillis());
		sysRoleMapper.insertSelective(sysRole);
	}

	@Override
	public void updateRole(SysRole sysRole) {
		sysRoleMapper.updateByPrimaryKeySelective(sysRole);
	}

	@Override
	public SysRole getSysRoleById(Long id) {
		return sysRoleMapper.selectByPrimaryKey(id);
	}

	@Override
	public void deleteSysRoleById(String ids) {
		String[] split = ids.split(",");
		for (String id : split) {
			sysRoleMapper.deleteByPrimaryKey(Long.parseLong(id));
		}
	}

	@Override
	public List<SysRole> getRoleListNoPage() {
		SysRoleExample example = new SysRoleExample();
		SysRoleExample.Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(1L);
		example.setOrderByClause(" id asc ");
		return sysRoleMapper.selectByExample(example);
	}

}
